# -*- coding: utf-8 -*-
from tornado.web import RequestHandler
import hashlib


class BaseHandler(RequestHandler):

    @property
    def db(self):
        return self.application.db

    def prepare(self):
        signature = self.get_argument("signature", "")
        timestamp = self.get_argument("timestamp", "")
        nonce = self.get_argument("nonce", "")
        echostr = self.get_argument("echostr", "")
        tmp = ['fovegage', timestamp, nonce]
        tmp.sort()
        tmp = "".join(tmp)
        s1 = hashlib.sha1()
        s1.update(tmp.encode())
        tmp = s1.hexdigest()
        if tmp == signature:
            self.write(echostr)
        else:
            self.write("非微信请求，拒绝访问!!!")

    def write_error(self, status_code, **kwargs):
        pass

    def set_default_headers(self):
        pass

    def initialize(self):
        pass

    def on_finish(self):
        pass

